package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.Messages;
import com.school.campus.service.IMessagesService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 单聊消息（存储一对一文字消息）Controller
 *
 * @author wwj
 * @date 2025-11-07
 */
@RestController
@RequestMapping("/campus/messages")
public class MessagesController extends BaseController {
    @Autowired
    private IMessagesService messagesService;

    /**
     * 查询单聊消息（存储一对一文字消息）列表
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:list')")
    @GetMapping("/list")
    public TableDataInfo list(Messages messages) {
        startPage();
        List<Messages> list = messagesService.selectMessagesList(messages);
        return getDataTable(list);
    }

    /**
     * 导出单聊消息（存储一对一文字消息）列表
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:export')")
    @Log(title = "单聊消息（存储一对一文字消息）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Messages messages) {
        List<Messages> list = messagesService.selectMessagesList(messages);
        ExcelUtil<Messages> util = new ExcelUtil<Messages>(Messages.class);
        util.exportExcel(response, list, "单聊消息（存储一对一文字消息）数据");
    }

    /**
     * 获取单聊消息（存储一对一文字消息）详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(messagesService.selectMessagesById(id));
    }

    /**
     * 新增单聊消息（存储一对一文字消息）
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:add')")
    @Log(title = "单聊消息（存储一对一文字消息）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Messages messages) {
        return toAjax(messagesService.insertMessages(messages));
    }

    /**
     * 修改单聊消息（存储一对一文字消息）
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:edit')")
    @Log(title = "单聊消息（存储一对一文字消息）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Messages messages) {
        return toAjax(messagesService.updateMessages(messages));
    }

    /**
     * 删除单聊消息（存储一对一文字消息）
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:remove')")
    @Log(title = "单聊消息（存储一对一文字消息）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messagesService.deleteMessagesByIds(ids));
    }

    /**
     * 清空聊天记录
     */
    @PreAuthorize("@ss.hasPermi('campus:messages:clear')")
    @Log(title = "单聊消息（存储一对一文字消息）", businessType = BusinessType.DELETE)
    @DeleteMapping("/clear/{conversationId}")
    public AjaxResult clear(@PathVariable Long conversationId) {
        return toAjax(messagesService.clear(conversationId));
    }
}
